//
//  Column2Controller.m
//  MY Project For Sport
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "Column2Controller.h"
#import "WebviewForNews.h"
#import "Column2HeaderView.h"
#import "CellForColumn2.h"
#import "NetworkingHandlerOfBlock.h"
#import "ModelForColumn2.h"
#import <SDWebImage/UIImageView+WebCache.h>


@interface Column2Controller ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)Column2HeaderView *customView;

@end

@implementation Column2Controller

- (void)dealloc {
    
    [_tableView release];
    [_array release];
    [_customView release];
    [_read release];
    [super dealloc];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    [self createTableView];
    
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    
}
- (void)handleData {

    self.array = [NSMutableArray array];
[NetworkingHandler networkingWithUrl:self.url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
   
  //  NSLog(@"%@", self.url);
    
    NSDictionary *dic = [result objectForKey:@"result"];
    NSDictionary *dict = [dic objectForKey:@"data"];
    NSDictionary *dicd = [dict objectForKey:@"feed"];
    
    [self.customView.backgroundView sd_setImageWithURL:[NSURL URLWithString:[dicd objectForKey:@"pic"]]];
    self.customView.labelForName.text = [dicd objectForKey:@"title"];
    self.customView.labelForSum.text = [dicd objectForKey:@"desc"];
    [self.customView.image sd_setImageWithURL:[NSURL URLWithString:[dicd objectForKey:@"pic"]]];
    self.customView.number.text = [self.read stringByAppendingFormat:@"订阅"];
    
    NSArray *array = [dicd objectForKey:@"data"];
    
    
    if ([array isEqual:[NSNull null]] == 0) {
        
        for (NSDictionary *dictt in array) {
            
            ModelForColumn2 *model = [[ModelForColumn2 alloc] init];
            
            [model setValuesForKeysWithDictionary:dictt];
            
            [self.array addObject:model];
            [model release];
            
            
        }
    }
    [self.tableView reloadData];
}];

}
- (void)createTableView {

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.customView = [[Column2HeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    
    self.tableView.tableHeaderView = self.customView;
    [_customView release];
    [self.tableView registerClass:[CellForColumn2 class] forCellReuseIdentifier:@"poolForColumn2"];
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellForColumn2 *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForColumn2"];
    
    
    
    [cell passModelForHotNews:[self.array objectAtIndex:indexPath.row]];
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return self.view.frame.size.height / 7;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForColumn2 *model = [self.array objectAtIndex:indexPath.row];
    
    WebviewForNews *web = [[WebviewForNews alloc] init];
    
    web.url = model.url;
    
    [self.navigationController pushViewController:web animated:NO];
    
    [web release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
